﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace hw1_2
{
    public partial class Form1 : Form
    {
        public int opp = 0;
        public int st = 10;//每题时间
        public int sq = 11;//题目数量
        public int n1 = 10;
        public int n2 = 10;
        public int score = 0;
        public Form1()
        {
            InitializeComponent();
            refresh();
            generate_new_question();
        }
        public void refresh()
        {
            this.time.Text = st.ToString() + "s";
            this.ques.Text = sq.ToString() + "道";
            this.scores.Text = score.ToString() + "分";
        }
        public void generate_new_question()
        {
            this.ans.Text = "";
            this.st = 10;
            this.sq -= 1;
            refresh();
            if (sq <= 0)
            {
                MessageBox.Show("最终得分为" + this.score.ToString(), "提示");
                this.Close();
            }
            Random random = new Random();
            this.n1 = random.Next(10);
            this.num1.Text = n1.ToString();
            this.n2 = random.Next(10);
            this.num2.Text = n2.ToString();
            this.opp = random.Next(2);
            if (this.opp == 0)
                this.op.Text = "+";
            else
                this.op.Text = "-";
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            st -= 1;
            this.time.Text = st.ToString() + "s";
            if (this.st < 0)
                generate_new_question();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int answer;
            if (int.TryParse(this.ans.Text, out answer))
            {
            }
            else
            {
                this.timer1.Stop();
                MessageBox.Show("请输入合法数字", "提示");
                this.timer1.Start();
                return;
            }
            if (this.opp == 0)//+
            {
                if (this.n1 + this.n2 == answer)
                {
                    this.score += 10;
                    generate_new_question();
                }
                else
                {
                    this.timer1.Stop();
                    MessageBox.Show("答案错误！正确答案为" + (this.n1 + this.n2).ToString(), "提示");
                    this.timer1.Start();
                    generate_new_question();
                }
            }
            else
            {
                if (this.n1 - this.n2 == answer)
                {
                    this.score += 10;
                    generate_new_question();
                }
                else
                {
                    this.timer1.Stop();
                    MessageBox.Show("答案错误！正确答案为" + (this.n1 - this.n2).ToString(), "提示");
                    this.timer1.Start();
                    generate_new_question();
                }
            }

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }
    }
}
